<?php

namespace App\Jobs;

use App\Facades\SamLogFacade;
use App\Models\UserGlobal;
use App\Models\UserLoanRequest;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\DB;
use Exception;
use Illuminate\Support\Facades\Log;

/**
 * Class LoanRequestRestartJob
 * Description:授信成功7天后处理任务类
 * Author: Sam
 * DateTime: 2019/1/8 9:18
 * @package App\Jobs
 */
class LoanRequestRestartJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $loan;

    /**
     * LoanRequestRestartJob constructor.
     * @param UserLoanRequest $loan
     */
    public function __construct(UserLoanRequest $loan)
    {
        $this->loan = $loan;
    }

    /**
     * Description:
     * Author: Sam
     * DateTime: 2019/1/7 17:25
     */
    public function handle()
    {
        $loan = $this->loan;
        if($loan->status == UserLoanRequest::STATUS_TIME_OUT){
            DB::beginTransaction();
            try
            {
                $loan->status = UserLoanRequest::STATS_RESTART;
                $loan->save();
                DB::commit();
                SamLogFacade::debug("授信成功记录【{$loan->id}】更改完全超时状态成功");
            }catch(Exception $exception)
            {
                SamLogFacade::error("授信成功记录【{$loan->id}】更改完全超时状态失败：".$exception->getMessage());
                DB::rollBack();
            }
        }
    }
}
